package com.shop.cereshop.app.controller.tencent.im;

import com.shop.cereshop.app.tencent.im.model.transfer.*;
import com.shop.cereshop.app.tencent.im.service.TransferOrderService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @description: 腾讯IM-转账接口
 * @author zdd
 * @date 2025/9/22 14:36
 * @version 1.0
 */
@RestController
@RequestMapping("/tim/transfer")
@Slf4j(topic = "TransferController")
@Api(value = "腾讯IM 转账接口", tags = "腾讯IM 转账接口")
public class TransferOrderController {

    @Resource
    private TransferOrderService transferOrderService;

    @ApiOperation(value = "创建转账订单")
    @PostMapping(value = "/create")
    public Result<TransferOrderVO> create(HttpServletRequest request, @Validated @RequestBody CreateTransferOrderParam param) {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        param.setSenderId(String.valueOf(user.getBuyerUserId()));
        param.setSenderName(user.getName());
//        log.info("创建转账订单CereBuyerUser：{}", JSON.toJSONString(user));
        return transferOrderService.create(param);
    }

    @ApiOperation(value = "发送转账消息")
    @PostMapping("/send")
    public Result<Void> send(HttpServletRequest request, @Validated @RequestBody SendTransferOrderParam param) {
        String ipAddress = IpUtils.getClientIpAddress(request);
        param.setIpAddress(ipAddress);
        return transferOrderService.send(param);
    }

    @ApiOperation(value = "领取转账")
    @PostMapping("/receive")
    public Result<Object> receive(HttpServletRequest request, @Validated @RequestBody ReceiveTransferOrderParam param) {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        TransferOrderDTO transferDTO = TransferOrderDTO.builder()
                .transferId(param.getTransferId())
                .receiverId(String.valueOf(user.getBuyerUserId()))
                .receiverName(user.getName())
                .receiverIp(IpUtils.getClientIpAddress(request))
                .build();
        return transferOrderService.receive(transferDTO);
    }

    @ApiOperation(value = "用户退回转账")
    @PostMapping("/refund")
    public Result<Object> refund(HttpServletRequest request, @Validated @RequestBody RefundTransferOrderParam param) {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        TransferOrderDTO transferDTO = TransferOrderDTO.builder()
                .transferId(param.getTransferId())
                .receiverId(String.valueOf(user.getBuyerUserId()))
                .receiverName(user.getName())
                .receiverIp(IpUtils.getClientIpAddress(request))
                .build();
        return transferOrderService.refundByUser(transferDTO);
    }

    @ApiOperation(value = "查询转账详情")
    @GetMapping("/detail/{transferId}")
    public Result<TransferOrderDetailVO> detail(@PathVariable String transferId) {
        return transferOrderService.detail(transferId);
    }

}
